import 'dart:io';

import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class Tools {
  static Options options = Options(
    baseUrl: "https://api.readtool.cn/",
//    baseUrl: "http://127.0.0.1:8080/",
    connectTimeout: 5000,
    contentType: ContentType.parse("application/x-www-form-urlencoded"),
  );
  static Dio dio = Dio(options);

  request(String path, Map<String, dynamic> body, {int times}) async {
    print(times);
    if (times == null) {
      times = 3;
    }

    try {
      var res = await dio.post(path,data: body);
      return res;
      return dio.post(path, data: body);
    } on DioError catch (e) {
      print(
          "network_Error");
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx and is also not 304.
      if (times == 0) {
        return null;
      }
      print("times"+times.toString());

      if (e.type == DioErrorType.CONNECT_TIMEOUT) {
        times--;
        var res = await request(path, body, times: times);
        return res;
      }
    }
//    return dio.post(path, data: body);
  }

  showToast(String msg) {
    return Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIos: 1,
        textcolor: '#ffffff');
  }

  Future<String> getDBPath() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, "read.db");
    return path;
  }
}
